package tongcheng.com.tongcheng.base.main.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yanzhenjie.nohttp.RequestMethod;

import org.json.JSONObject;

import butterknife.Bind;
import tongcheng.com.tongcheng.ConStans;
import tongcheng.com.tongcheng.R;
import tongcheng.com.tongcheng.base.BaseActivity;
import tongcheng.com.tongcheng.network.CallServer;
import tongcheng.com.tongcheng.network.MyRequest;
import tongcheng.com.tongcheng.network.OnCallBack;
import tongcheng.com.tongcheng.utils.Setting;
import tongcheng.com.tongcheng.utils.ShowToast;

public class ZhiFuBaoTiXianActivity extends BaseActivity {


    @Bind(R.id.back)
    ImageView mBack;
    @Bind(R.id.title)
    TextView mTitle;
    @Bind(R.id.title_bar)
    RelativeLayout mBg;
    @Bind(R.id.et_tiXianZhangHu)
    EditText et_tiXianZhangHu;
    @Bind(R.id.et_zhenShiXingMing)
    EditText et_zhenShiXingMing;
    @Bind(R.id.tixian_ok)
    Button mTixianOk;

    private String jine = "";


    @Override
    public Integer initResid() {
        return R.layout.activity_zhi_fu_bao;
    }

    @Override
    public void initCreate(Bundle savedInstanceState) {
        settitlebar(mBg);
        mTitle.setText("支付宝提现");
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        jine = getIntent().getStringExtra("jine");
        et_tiXianZhangHu.setText(Setting.getZhiFuBaoZhangHu());
        et_zhenShiXingMing.setText(Setting.getZhiFuBaoZhangHuName());

        mTixianOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String zhanghu = et_tiXianZhangHu.getText().toString().trim();
                String name = et_zhenShiXingMing.getText().toString().trim();
                if(!TextUtils.isEmpty(zhanghu) && !TextUtils.isEmpty(name)){
                    zhiFuBaoTiXian(jine,zhanghu,name);
                }else {
                    ShowToast.showToast("账户及姓名不能为空");
                }

            }
        });

    }

    private void zhiFuBaoTiXian(String num , final String account, final String real_name) {
        MyRequest request = new MyRequest(ConStans.ZhiFuBaoTiXianUrl, RequestMethod.POST);
        request.add("userid", Setting.getId());
        request.add("payee_account",account + "");
        request.add("amount",num + "");
        request.add("payee_real_name",real_name + "");
        request.add("remark","丹舟同城");
        CallServer.getInstance().addPost(this, request, 3, new OnCallBack() {
            @Override
            public void OnSuccess(int what, String json, Object ject) {
                Log.e("支付宝提现",json + "");
                try {
                    JSONObject object = new JSONObject(json);
                    if(object.getInt("status") == 1){
                        JSONObject object1 = object.getJSONObject("data");
                        String balance = object1.getString("balance");
                        Setting.setYuE(balance);
                        Setting.setZhiFuBaoZhangHu(account);
                        Setting.setZhiFuBaoZhangHuName(real_name);
                        finish();
                    }else if(object.getInt("status") == 0){
                        JSONObject object1 = object.getJSONObject("data");
                        String text = object1.getString("sub_msg");
                        ShowToast.showToast(text);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

}
